<?php

namespace App\Process;

use App\Utility\Cache;
use EasySwoole\Component\Process\AbstractProcess;

class StatisticsCounts extends AbstractProcess
{
    private $isRun = false;
    public function run($arg){
        //定时200ms检测有没有任务，有的话就while死循环执行
        $this->addTick(60000,function (){
            if(!$this->isRun){
                $this->isRun = true;
                go(function (){
                    try{
                        $cache = new Cache();
                        $AppletsModel = new \App\Model\AppletsModel;
                        $tableList = $AppletsModel->select(['name', 'title','manages'])->get();

                        $date = date('Y-m');
                        $day = date('Y-m-d');
                        $result = [];
                        $resultDay = [];
                        foreach ($tableList as $table) {
                            $modelName = '\App\Model\Counts' . ucfirst($table['name']) . 'Model';
                            if (!class_exists($modelName)) {
                                // 开发阶段防止model不存在
                                continue;
                            }
                            $model = new $modelName;

                            $data = $model->select(['sum(view) as view', 'sum(share_in) as share_in', 'date'])
                                ->whereLike('date', $date)
                                ->first();
                            $dataDay = $model->select(['sum(view) as view_day', 'sum(share_in) as share_in_day', 'date'])
                                ->where('date', $day)
                                ->first();

                            // 释放model
                            unset($model);

                            $share_in = $data['share_in'] ?? 0;
                            $view = $data['view'] ?? 0;

                            $share_in_day = $dataDay['share_in_day'] ?? 0;
                            $view_day = $dataDay['view_day'] ?? 0;
                            if ($share_in == 0 && $view == 0 && $share_in_day == 0 && $view_day == 0) {
                                continue;
                            }
                            $proportion = 0;
                            $proportionDay = 0;
                            if ($share_in) {
                                $proportion = bcdiv($share_in, $view, 4) * 100;
                                $proportion = round($proportion, 2);
                            }
                            if ($share_in_day) {
                                $proportionDay = bcdiv($share_in_day, $view_day, 4) * 100;
                                $proportionDay = round($proportionDay, 2);
                            }

                            $result[] = [
                                'name' => $table['name'],
                                'title' => $table['title'],
                                'view' => $view,
                                'share_in' => $share_in,
                                'date' => $date,
                                'proportion' => $proportion,
                                'person' => $table['manages']
                            ];
                            $resultDay[] = [
                                'name' => $table['name'],
                                'title' => $table['title'],
                                'view' => $view_day,
                                'share_in' => $share_in_day,
                                'date' => $date,
                                'proportion' => $proportionDay,
                                'person' => $table['manages']
                            ];

                            $proportionData = array_column($result,'proportion');
                            array_multisort($proportionData,SORT_DESC, $result);
                            $proportionDataDay = array_column($resultDay,'proportion');
                            array_multisort($proportionDataDay,SORT_DESC, $resultDay);
                        }

                        $cache->set('statistics_counts', $result);
                        $cache->set('statistics_counts_day', $resultDay);
                        unset($cache, $AppletsModel);
                    }catch (\Throwable $throwable){
                    }
                    $this->isRun = false;
                });
            }
        });
    }

    public function onShutDown()
    {
        // TODO: Implement onShutDown() method.
    }

    public function onReceive(string $str, ...$args)
    {
        // TODO: Implement onReceive() method.
    }
}
